home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Diamond Collection / The Diamond Collection (Software Vault)(Digital Impact).ISO / cdr44 / newmat08.zip / NEWMATRM.CPP < prev    next >
C/C++ Source or Header  |  1995-01-11  |  4KB  |  135 lines

  1. //$$newmatrm.cpp                         rectangular matrix operations
  2.  
  3. // Copyright (C) 1991,2,3,4: R B Davies
  4.  
  5. #include "include.h"
  6.  
  7. #include "newmat.h"
  8. #include "newmatrm.h"
  9.  
  10.  
  11. // operations on rectangular matrices
  12.  
  13.  
  14. void RectMatrixRow::Reset (const Matrix& M, int row, int skip, int length)
  15. {
  16.    RectMatrixRowCol::Reset
  17.       ( M.Store()+row*M.Ncols()+skip, length, 1, M.Ncols() );
  18. }
  19.  
  20. void RectMatrixRow::Reset (const Matrix& M, int row)
  21. {
  22.    RectMatrixRowCol::Reset( M.Store()+row*M.Ncols(), M.Ncols(), 1, M.Ncols() );
  23. }
  24.  
  25. void RectMatrixCol::Reset (const Matrix& M, int skip, int col, int length)
  26. {
  27.    RectMatrixRowCol::Reset
  28.       ( M.Store()+col+skip*M.Ncols(), length, M.Ncols(), 1 );
  29. }
  30.  
  31. void RectMatrixCol::Reset (const Matrix& M, int col)
  32.    { RectMatrixRowCol::Reset( M.Store()+col, M.Nrows(), M.Ncols(), 1 ); }
  33.  
  34.  
  35. Real RectMatrixRowCol::SumSquare() const
  36. {
  37.    long_Real sum = 0.0; int i = n; Real* s = store; int d = spacing;
  38.    while (i--) { sum += (long_Real)*s * *s; s += d; }
  39.    return (Real)sum;
  40. }
  41.  
  42. Real RectMatrixRowCol::operator*(const RectMatrixRowCol& rmrc) const
  43. {
  44.    long_Real sum = 0.0; int i = n;
  45.    Real* s = store; int d = spacing;
  46.    Real* s1 = rmrc.store; int d1 = rmrc.spacing;
  47.    if (i!=rmrc.n)
  48.    {
  49.       Tracer tr("newmatrm");
  50.       Throw(InternalException("Dimensions differ in *"));
  51.    }
  52.    while (i--) { sum += (long_Real)*s * *s1; s += d; s1 += d1; }
  53.    return (Real)sum;
  54. }
  55.  
  56. void RectMatrixRowCol::AddScaled(const RectMatrixRowCol& rmrc, Real r)
  57. {
  58.    int i = n; Real* s = store; int d = spacing;
  59.    Real* s1 = rmrc.store; int d1 = rmrc.spacing;
  60.    if (i!=rmrc.n)
  61.    {
  62.       Tracer tr("newmatrm");
  63.       Throw(InternalException("Dimensions differ in AddScaled"));
  64.    }
  65.    while (i--) { *s += *s1 * r; s += d; s1 += d1; }
  66. }
  67.  
  68. void RectMatrixRowCol::Divide(const RectMatrixRowCol& rmrc, Real r)
  69. {
  70.    int i = n; Real* s = store; int d = spacing;
  71.    Real* s1 = rmrc.store; int d1 = rmrc.spacing;
  72.    if (i!=rmrc.n)
  73.    {
  74.       Tracer tr("newmatrm");
  75.       Throw(InternalException("Dimensions differ in Divide"));
  76.    }
  77.    while (i--) { *s = *s1 / r; s += d; s1 += d1; }
  78. }
  79.  
  80. void RectMatrixRowCol::Divide(Real r)
  81. {
  82.    int i = n; Real* s = store; int d = spacing;
  83.    while (i--) { *s /= r; s += d; }
  84. }
  85.  
  86. void RectMatrixRowCol::Negate()
  87. {
  88.    int i = n; Real* s = store; int d = spacing;
  89.    while (i--) { *s = - *s; s += d; }
  90. }
  91.  
  92. void RectMatrixRowCol::Zero()
  93. {
  94.    int i = n; Real* s = store; int d = spacing;
  95.    while (i--) { *s = 0.0; s += d; }
  96. }
  97.  
  98. void ComplexScale(RectMatrixCol& U, RectMatrixCol& V, Real x, Real y)
  99. {
  100.    int n = U.n;
  101.    if (n != V.n)
  102.    {
  103.       Tracer tr("newmatrm");
  104.       Throw(InternalException("Dimensions differ in ComplexScale"));
  105.    }
  106.    Real* u = U.store; Real* v = V.store; 
  107.    int su = U.spacing; int sv = V.spacing;
  108.    while (n--)
  109.    {
  110.       Real z = *u * x - *v * y;  *v =  *u * y + *v * x;  *u = z;
  111.       u += su;  v += sv;
  112.    }
  113. }
  114.  
  115. void Rotate(RectMatrixCol& U, RectMatrixCol& V, Real tau, Real s)
  116. {
  117.    //  (U, V) = (U, V) * (c, s)  where  tau = s/(1+c), c^2 + s^2 = 1
  118.    int n = U.n;
  119.    if (n != V.n)
  120.    {
  121.       Tracer tr("newmatrm");
  122.       Throw(InternalException("Dimensions differ in Rotate"));
  123.    }
  124.    Real* u = U.store; Real* v = V.store; 
  125.    int su = U.spacing; int sv = V.spacing;
  126.    while (n--)
  127.    {
  128.       Real zu = *u; Real zv = *v;
  129.       *u -= s * (zv + zu * tau); *v += s * (zu - zv * tau);
  130.       u += su;  v += sv;
  131.    }
  132. }
  133.  
  134.  
  135.